﻿using System;
using System.Diagnostics;
using System.Net;
using System.ComponentModel;

namespace ViewModel
{
    /// <summary>
    /// The base class for all classes of the ViewModel
    /// </summary>
    public class ViewModelBase : INotifyPropertyChanged
    {
        protected String displayName;

        /// <summary>
        /// The displayed name of the viewmodel
        /// </summary>
        public String DisplayName
        {
            get { return this.displayName; }
            set 
            { 
                this.displayName = value;
                OnPropertyChanged("DisplayName");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        
        /// <summary>
        /// Called when a property has changed.
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            // check the property name
            this.CheckPropertyName(propertyName);

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
        /// <summary>
        /// Verify that the property matches a real instance property
        /// To avoid errors of wrong property name.
        /// </summary>
        /// <param name="propertyName"></param>
        [Conditional("DEBUG")]
        [DebuggerStepThrough]
        public void CheckPropertyName(string propertyName)
        {
            if (this.GetType().GetProperty(propertyName) == null)
            {
                string msg = "Invalid property name: " + propertyName;
                Debug.WriteLine(msg);
            }
        }

    }
}
